package org.etom.impl.ui.wizards;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.etom.impl.ui.util.Template;

public class UploadWizardPage extends AbstractUploadWizardPage {

  private String fileName;
  
  protected UploadWizardPage(IStructuredSelection selection, String fileNameTemplate) {
    super(selection);
    this.fileName = fileNameTemplate;
  }

  @Override
  protected void createDestinationGroup(Composite parent) {
    super.createDestinationGroup(parent);
    fileNameTemplateText.setText(fileName);
  }
  
  @Override
  public void handleEvent(Event event) {
    //nothing to do here
  }
  
  @Override
  public boolean canFlipToNextPage() {
    List<?> selectedResources = getSelectedResources();
    return ((selectedResources != null) && (selectedResources.size() > 0));
  }

  @Override
  public boolean isPageComplete() {
    return canFlipToNextPage();
  }

  @Override
  public void createControl(Composite parent) {
    super.createControl(parent);
    setPageComplete(false);
  }
  
  public Template getFileNameTemplate() {
    return Template.getInstance(fileNameTemplateText.getText());
  }
  
  public List<?> performFinish(String fileName) throws CoreException, IOException {
    List<?> selectedResources = getSelectedResources();
    if ((selectedResources == null)
      ||(selectedResources.isEmpty())) {
      throw new IllegalStateException(WizardsMessages.Upload_Wizard_Page_No_Selected_Resources);
    }
    return selectedResources;
  }

}
